﻿using BloodApp.Common;
using BloodApp.ViewModel;
using Microsoft.Extensions.DependencyInjection;
using System.ComponentModel.Design;
using System.Windows;
using Application = System.Windows.Application;

namespace BloodApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public new static App Current => (App)Application.Current;
        public IServiceProvider Services { get; }
        public App()
        {
            //切换语言
            GlobalService.ChangeLanguageService.OnChangeLanguage += (language) =>
            {
                var path = @"pack://application:,,,/BloodApp;component/Resources/zh.xaml";
                switch (language)
                {
                    case "zh":
                        path = @"pack://application:,,,/BloodApp;component/Resources/zh.xaml";
                        break;
                    case "en":
                        path = @"pack://application:,,,/BloodApp;component/Resources/en.xaml";
                        break;
                    default:
                        path = @"pack://application:,,,/BloodApp;component/Resources/zh.xaml";
                        break;
                }
                Resources.MergedDictionaries[0].Source = new Uri(path);
            };
            GlobalService.ChangeLanguageService.OnLoadLanguage += (key) =>
            {
                return Resources.MergedDictionaries[0][key].ToString();
            };
            //注册全局异常事件
            AppDomain.CurrentDomain.ProcessExit += (sender, args) =>
            {
                LogService.Logger.Info("程序退出！");
            };
            AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
            {
                if (args.ExceptionObject is Exception ex && Dispatcher != null)
                {
                    HandyControl.Controls.MessageBox.Error(ex.Message, "主线程未捕获的异常");
                }
            };
            DispatcherUnhandledException += (sender, args) =>
            {
                HandyControl.Controls.MessageBox.Error(args.Exception.Message, "子线程未捕获的异常");
                args.Handled = true;
            };
            TaskScheduler.UnobservedTaskException += (sender, args) =>
            {
                args.SetObserved();
            };
            // 依赖注入
            Services = ConfigureServices();
            this.InitializeComponent();
        }

        private string ChangeLanguageService_OnLoadLanguage(string key)
        {
            return Resources.MergedDictionaries[0][key].ToString();
        }

        private void ChangeLanguageService_OnChangeLanguage(string language)
        {
            var path = @"./Resources/zh.xaml";
            switch (language)
            {
                case "zh":
                    path = @"./Resources/zh.xaml";
                    break;
                case "en":
                    path = @"./Resources/en.xaml";
                    break;
                default:
                    path = @"./Resources/zh.xaml";
                    break;
            }
            Resources.MergedDictionaries[0].Source = new Uri(path);
        }

        private static IServiceProvider ConfigureServices()
        {
            var services = new ServiceCollection();
            //services.AddSingleton<IDataAccess, DataAccess>();
            services.AddSingleton<MainWindowVM>();
            return services.BuildServiceProvider();
        }
    }

}
